$(function () {
  // 是否加载完成
  var loadBannerEnd = false
  var loadMemberEnd = false
  var loadEnterpriseEnd = false
  $(".container-fluid").hide()
  // 设置横向滚动
  function setScroll() {
    var element = $(".column-3 .column-cont");
    element.on("wheel", function (event) {
      if (element.get(0).scrollWidth.toFixed(0) > element.innerWidth().toFixed(0)) {
        event.preventDefault();
        var scrollAmount = event.originalEvent.deltaY;
        element.scrollLeft(element.scrollLeft() + scrollAmount);
      }
    });
  }
  // 获取轮播图
  function getBanner() {
    $.ajax({
      method: 'GET',
      url: adminPath + '/api/wdsxh/banner/index',
      headers: {
        page: 1
      },
      success: function (res) {
        loadBannerEnd = true
        if (loadBannerEnd && loadMemberEnd && loadEnterpriseEnd) {
          $(".container-fluid").show()
        }
        if (res.code == 1) {
          var indicatorsHtml = ""
          var innerHtml = ""
          for (var i in res.data) {
            indicatorsHtml += `<li data-target="#carouselExampleIndicators" data-slide-to="${i}" ${i == 0 ? 'class="active"' : ''}></li>`
            innerHtml += `
              <div class="carousel-item ${i == 0 ? 'active' : ''}">
                <img src="${res.data[i].pc_image}" class="w-100 normal">
                <img src="${res.data[i].image}" class="w-100 mobile">
              </div>
            `
          }
          $("#carouselExampleIndicators .carousel-indicators").html(indicatorsHtml)
          $("#carouselExampleIndicators .carousel-inner").html(innerHtml)
        } else {
          console.error(res.msg)
        }
      },
    })
  }
  // 获取会员风采
  function getMember() {
    $.ajax({
      method: 'GET',
      url: adminPath + '/api/wdsxh/member/lists',
      data: {
        page: 1,
        limit: 20,
      },
      success: function (res) {
        loadMemberEnd = true
        if (loadBannerEnd && loadMemberEnd && loadEnterpriseEnd) {
          $(".container-fluid").show()
        }
        if (res.code == 1) {
          var memberHtml = ""
          for (var i in res.data.list) {
            memberHtml += `
              <a href="membership/membership_detail.html?from=2&id=${res.data.list[i].id}" class="cont-item">
                <div class="avatar"><img src="${res.data.list[i].picture}"></div>
                <div class="post">${res.data.list[i].level.name}</div>
                <div class="name">${res.data.list[i].name}</div>
              </a>
            `
          }
          $(".column-3 .column-cont").html(memberHtml)
        } else {
          console.error(res.msg)
        }
      },
    })
  }
  // 获取会员企业
  function getEnterprise() {
    $.ajax({
      method: 'GET',
      url: adminPath + '/api/wdsxh/member/company',
      data: {
        page: 1,
        limit: 6,
      },
      success: function (res) {
        loadEnterpriseEnd = true
        if (loadBannerEnd && loadMemberEnd && loadEnterpriseEnd) {
          $(".container-fluid").show()
        }
        if (res.code == 1) {
          var enterpriseHtml = ""
          for (var i in res.data.list) {
            enterpriseHtml += `
              <a href="enterprise/enterprise_detail.html?from=2&id=${res.data.list[i].id}" class="cont-item">
                <div class="image">
                  <img src="${res.data.list[i].company_logo}">
                  <div class="post">${res.data.list[i].level.name}单位</div>
                </div>
                <div class="name">${res.data.list[i].company_name}</div>
              </a>
            `
          }
          $(".column-4 .column-cont").html(enterpriseHtml)
        } else {
          console.error(res.msg)
        }
      },
    })
  }
  // 获取资讯分类
  function getNewsType() {
    $.ajax({
      method: 'GET',
      url: adminPath + '/api/wdsxh/article/cat',
      success: function (res) {
        if (res.code == 1) {
          if (res.data[0]) {
            $(".column-5").css("display", "flex")
            $(".column-5 .column-title .title").text(res.data[0].name)
            getNewsList(res.data[0].id, 1)
          }
          if (res.data[1]) {
            $(".column-6").css("display", "flex")
            $(".column-6 .column-left .column-title .title").text(res.data[1].name)
            getNewsList(res.data[1].id, 2)
          }
          if (res.data[2]) {
            $(".column-6 .column-right").show()
            $(".column-6 .column-right .column-title .title").text(res.data[2].name)
            getNewsList(res.data[2].id, 3)
          }
        } else {
          console.error(res.msg)
        }
      },
    })
  }
  // 获取资讯列表
  function getNewsList(id, type) {
    $.ajax({
      method: 'GET',
      url: adminPath + '/api/wdsxh/article/lists',
      data: {
        cat_id: id,
        page: 1,
        limit: 5,
      },
      success: function (res) {
        if (res.code == 1) {
          var newsHtml = ""
          for (var i in res.data) {
            newsHtml += `
              <a href="news/news_detail.html?from=2&id=${res.data[i].id}" class="item flex justify-content-between">
                <div class="item-title">${res.data[i].title}</div>
                <div class="item-date">${res.data[i].createtime}</div>
              </a>
            `
          }
          if (type == 1) {
            $(".column-5 .column-cont").html(newsHtml)
          } else if (type == 2) {
            $(".column-6 .column-left .column-cont").html(newsHtml)
          } else if (type == 3) {
            $(".column-6 .column-right .column-cont").html(newsHtml)
          }
        } else {
          console.error(res.msg)
        }
      },
    })
  }
  // 调用方法
  setScroll()
  getBanner()
  getMember()
  getEnterprise()
  getNewsType()
});